﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Meta
{
	class Program
	{
		[STAThread]
		public static void Main(string[] args)
		{
			if (args.Length != 0)
			{
				if (args[0] == "-test")
				{
					Interpreter.UseConsole();
					new MetaTest().Run();
				}
				else if (args[0] == "-nprof")
				{
					Interpreter.Run(Path.Combine(Interpreter.InstallationPath, @"libraryTest.meta"), new DictionaryMap());
				}
				else if (args[0] == "-profile")
				{
					Interpreter.UseConsole();
					Interpreter.profiling = true;
					Interpreter.Run(Path.Combine(Interpreter.InstallationPath, @"game.meta"), new DictionaryMap());
					List<object> results = new List<object>(Map.calls.Keys);
					results.Sort(delegate(object a, object b)
					{
						return Map.calls[b].time.CompareTo(Map.calls[a].time);
					});
					foreach (object e in results)
					{
						Console.WriteLine(e.ToString() + "    " + Map.calls[e].time + "     " + Map.calls[e].calls);
					}
				}
				else if (args[0] == "-performance")
				{
					Interpreter.UseConsole();
					Interpreter.Run(Path.Combine(Interpreter.InstallationPath, @"learning.meta"), new DictionaryMap());
				}
				else if (args[0].Trim().StartsWith("meta:"))
				{
					Map directory = new DirectoryMap(Directory.GetCurrentDirectory());
					directory.Scope = Versions.versions;

					Map callable = Parser.ParseExpression(args[0].Trim().Substring("meta:".Length), "console");
					callable.Scope = directory;
					Map result = callable.Call(EmptyMap.Empty);

				}
				else
				{
					Map directory = new DirectoryMap(Directory.GetCurrentDirectory());
					directory.Scope = Versions.versions;
					string fileName = args[0].Trim('"');
					if (File.Exists(fileName))
					{
						try
						{
							Interpreter.Run(fileName, new DictionaryMap());
						}
						catch (Exception e)
						{
							Console.WriteLine(e.Message + "\n" + e.StackTrace);
							Console.ReadLine();
						}
					}
					else
					{
						Console.WriteLine("File " + fileName + " not found.");
						Console.ReadLine();
					}
				}
			}
		}
	}
}
